<template>
  <div class="pages personMg">
    <div class="pageLevel" v-if='!sub'>
      <!-- 操作框 -->
      <div class="operationBox tableSearch">
        <!-- <el-button type="primary" :disabled="dis" @click="goBack()" size="mini">返回</el-button> -->
<!--        <el-button type="primary" :disabled="dis" @click="goDetails({showTab:'2'})" size="mini">新增</el-button>-->
<!--        <el-button type="primary" :disabled="dis" @click="deleteRows()" size="mini">删除</el-button>-->
<!--        <el-button type="primary" @click="sync()" :disabled="dis" size="mini">同步到用户</el-button>-->
      </div>
      <!-- 搜索框 -->
      <div class="tableSearch">
        <el-form label-width="100px" class="demo-ruleForm">
          <el-form-item label="" label-width="15px">
            <el-input class="input" v-model="selectdata.keyword" size="small" placeholder="请输入员工号、姓名、手机号" @keyup.enter.native='searchSelect' style='width: 250px'></el-input>
          </el-form-item>
          <el-form-item label="" label-width="15px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="searchSelect" :disabled="dis" class="Button">查询</el-button>
            <!-- <el-button type="primary" icon="el-icon-search" size="mini" @click="showmore=!showmore" :disabled="dis">高级查询</el-button> -->
            <el-button v-if='setRole("新增")' type="primary" :disabled="dis" @click="goDetails({showTab:'2'})" size="mini">新增</el-button>
            <el-button v-if='setRole("删除")' type="primary" :disabled="dis" @click="deleteRows()" size="mini">删除</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 高级搜索 -->
      <div class="tableSearch" v-show="showmore">
        <el-form label-width="100px" class="demo-ruleForm">
          <el-form-item label="是否启用:" label-width="75px" style="margin-left:40px">
            <el-select v-model="selectdata.status" placeholder="全部" size="small">
              <el-option label="全部" value=""></el-option>
              <el-option label="启用" value="0"></el-option>
              <el-option label="停用" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="会员分类:" label-width="75px" style="margin-left:40px">
            <el-select v-model="selectdata.typeId" placeholder="个人会员" size="small">
              <el-option v-for="item in userType" :key="item.id" :value="item.id" :label="item.name"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <!-- 表格 -->
      <div class="tableMain">
        <el-table ref="multipleTable" :data="tableData" @selection-change="handleSelectionChange" stripe style="width: 100%">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="code" label="编号" align="center"></el-table-column>
          <el-table-column prop="name" label="姓名" align="center"></el-table-column>
          <el-table-column prop="phone" label="手机号" align="center"></el-table-column>
          <el-table-column prop="typeName" label="人员分类" align="center"></el-table-column>
          <el-table-column prop="isQiyong" label="是否启用" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.status==0">启用</span>
              <span v-else>已停用</span>
            </template>
          </el-table-column>
<!--          <el-table-column prop="syncState" label="是否同步" align="center">-->
<!--            <template slot-scope="scope">-->
<!--              <span v-if="scope.row.syncState==0">未同步</span>-->
<!--              <span v-else-if="scope.row.syncState==1">已同步</span>-->
<!--              <span v-else> </span>-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column prop="orgName" label="所属组织" align="center"></el-table-column>-->
          <el-table-column align="center" label="操作" width="180">
            <template slot-scope="scope">
              <el-button size="small" :disabled="dis" @click="goDetails({showTab:'1',id:scope.row.id})" type="text" class="addDetail">查看</el-button>
              <el-button size="small" v-if='setRole("修改")' :disabled="dis" @click="goDetails({showTab:'3',id:scope.row.id})" type="text" class="addDetail">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 页码 -->
        <div class="pageblock" v-if='total'>
          <el-button size="small" type="primary" class="topage" @click="toPage">确定</el-button>
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[20,30]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
      </div>
    </div>
    <div class="pageLevel" v-else>
      <router-view></router-view>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
import util from '@/util'
import {
  findEmployeeListPage,
  deleteEmployeeById,
  syncEmplToUser
} from "@/api";
export default {
  computed: mapState([
    "merchantId",
    "sub",
    'power',
    "isReLoadData"]),
  components: {},
  data() {
    return {
      dis: false,
      // 搜索框
      selectdata: {
        keyword: "",
        status: "",
        typeId: ""
      },
      selectdatas: {
        keyword: "",
        status: "",
        typeId: ""
      },
      searchData: "",
      userType: [],
      showmore: false,
      tableData: [],
      multipleSelection: [],
      // 分页开始
      total: 0,
      pageSize: 20,
      totalItems: 0,
      currentPage: 1
    };
  },
  mounted() {
    this.tableget(this.pageSize, 1)
  },
  watch: {
    sub: {
      handler: function(val, oldVal){
        if (oldVal != null && val == false) {
          this.tableget(this.pageSize, 1)
        }
      },
      deep: true
    },
    isReLoadData: function(val) {
      if (val == true) {
        this.tableget(20, 1);
        this.$store.commit("SET_ISRELOADDATA", false);
      }
    },
    deep: true
  },
  methods: {
    //配置权限
    setRole (key) {
      return util.selectTitle(this.power, key)
    },
    // 获取表格数据
    tableget(pageSize, pageNum) {
      let data = {
        pageNum: pageNum,
        pageSize: pageSize,
        shopId: this.merchantId,
        keyword: this.selectdatas.keyword,
        status: this.selectdatas.status,
        type: this.selectdatas.typeId
      };
      findEmployeeListPage(data).then(res => {
          this.total = res.data.pageTotal;
          this.tableData = res.data.list;
      });
    },
    // 点击返回按钮
    goBack() {
      window.history.go(-1);
      return false;
    },
    // 点击新增按钮
    goDetails(parames) {
      util.routerTo('staff-details',parames)
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 点击删除按钮
    deleteRows() {
      var arr = [];
      for (let i = 0; i < this.multipleSelection.length; i++) {
        arr.push(this.multipleSelection[i].id);
      }
      let postdata = {
        id: arr.toString()
      };
      if (arr.length == 0) {
        this.$message({
          type: "error",
          message: "请选择员工"
        });
      } else {
        deleteEmployeeById(postdata).then(res => {
          if(res.code == 200){
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.tableget(20, 1);
          }else {
            this.$message({
              type: "error",
              message: res.msg
            });
          }
        });
      }
    },
    // 点击查询按钮
    searchSelect() {
      this.selectdatas = this.selectdata;
      this.tableget(20, 1);
    },
    //跳页
    toPage() {
      this.tableget(this.pageSize, this.currentPage);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.tableget(this.pageSize, 1);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.tableget(this.pageSize, this.currentPage);
    },
    //同步到用户
    sync() {
      if (this.multipleSelection.length == 0) {
        this.$message({
          message: "请选择员工",
          type: "error"
        });
      } else {
        this.$confirm("确实同步员工到用户?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            let sendarr = [];
            this.multipleSelection.forEach(element => {
              sendarr.push(element.id);
            });
            let dataStr = sendarr.join();
            let sendData = {
              orgId: this.compId,
              id: dataStr.toString()
            };
            syncEmplToUser(sendData).then(res => {
              this.$message({
                type: "success",
                message: "同步成功!"
              });
              this.tableget(20, 1);
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消同步"
            });
          });
      }
    }
  }
};
</script>
<style scoped>
.showmore {
  display: block;
}
</style>

